package ligafutbol;

import javax.swing.*;
import javax.swing.tree.*;
import java.util.*;
import clases.*;
import java.util.ArrayList;

public class frmNuevaClasificacion extends javax.swing.JFrame {

    /** Creates new form frmNuevaClasificacion */
    public frmNuevaClasificacion() {
        initComponents();

        ArrayList<Equipo>lista = Equipo.obtenerEquipo();
        miModeloTabla mmt = new miModeloTabla();
        mmt.rellenarDatos(lista);
        this.tbClasificacion.setModel(mmt);
        this.tbClasificacion.setRowHeight(22);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lbTitulo = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tbClasificacion = new javax.swing.JTable();
        btnVolver = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        lbTitulo.setText("CLASIFICACIÓN LIGA");

        tbClasificacion.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        jScrollPane1.setViewportView(tbClasificacion);

        btnVolver.setText("<< Volver");
        btnVolver.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnVolverActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 636, Short.MAX_VALUE)
                            .addComponent(lbTitulo, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 636, Short.MAX_VALUE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(287, 287, 287)
                        .addComponent(btnVolver)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(29, 29, 29)
                .addComponent(lbTitulo, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 371, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(btnVolver)
                .addContainerGap(22, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnVolverActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnVolverActionPerformed
        // TODO add your handling code here:
        this.setVisible(false);
    }//GEN-LAST:event_btnVolverActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void inicio() {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new frmNuevaClasificacion().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnVolver;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lbTitulo;
    private javax.swing.JTable tbClasificacion;
    // End of variables declaration//GEN-END:variables

    class miModeloTabla extends javax.swing.table.AbstractTableModel{

        Object datos[][]= new Object[][]{};

        String columns[] = new String[]{"Nº","Equipo", "PJ","PG", "PP", "PE", "GC", "GF", "Puntos", "Dif. Goles"};

        Class tipo [] = new Class[]{java.lang.String.class, 
                                    java.lang.String.class,
                                    java.lang.Integer.class,
                                    java.lang.Integer.class,
                                    java.lang.Integer.class,
                                    java.lang.Integer.class,
                                    java.lang.Integer.class,
                                    java.lang.Integer.class,
                                    java.lang.Integer.class,
                                    java.lang.Integer.class};

        Boolean editables[] = new Boolean[]{false, false, false, false, false, false, false, false, false, false};

        private void rellenarDatos(ArrayList<Equipo> lista){
            Iterator it = lista.iterator();
            Object d [][] = new Object[lista.size()][columns.length];
            int i = 0;
            while(it.hasNext()){
                Equipo e = (Equipo)it.next();
                d[i][0] = i+1;
                d[i][1] = e.getNombre();
                d[i][2] = e.getPartidosJugados();
                d[i][3] = e.getPartidosGanado();
                d[i][4] = e.getPartidosEmpatado();
                d[i][5] = e.getPartidosPerdido();
                d[i][6] = e.getGolesCasa();
                d[i][7] = e.getGolesFuera();
                d[i][8] = e.getPuntos();
                d[i][9] = e.getNumGoles();
                i++;
            }

            datos = d;
        }

        @Override
        public String getColumnName(int i){
            return columns[i];
        }

        @Override
        public int getRowCount() {
            return datos.length;
        }

        @Override
        public int getColumnCount() {
            return columns.length;
        }

        @Override
        public boolean isCellEditable(int i, int a){
            return editables[i];
        }

        @Override
        public Object getValueAt(int rowIndex, int columnIndex) {
            return datos[rowIndex][columnIndex];
        }

        @Override
        public void setValueAt(Object obj, int rowIndex, int columnIndex){
            datos[rowIndex][columnIndex] = obj;
        }


    }
}
